//
//  MoodView.m
//  jishiben
//
//  Created by 李钰 on 2017/11/28.
//  Copyright © 2017年 jishiben. All rights reserved.
//

#import "MoodView.h"
#import "MyDbTools.h"
@interface MoodView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSArray * dataSource;
@end
@implementation MoodView

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame: frame]) {
        NSMutableArray * arr = [NSMutableArray arrayWithArray: [MyDbTools getMoodTitleList]];
        [arr addObject: STR(@"Cancel")];
        self.dataSource = arr;
        UIButton * btn = [[UIButton alloc] initWithFrame: self.bounds];
        [btn addTarget: self action: @selector(onTap) forControlEvents: UIControlEventTouchUpInside];
        [self addSubview: btn];
        [self addSubview: self.tableView];
        [self.tableView reloadData];
        self.backgroundColor = RGBA(0, 0, 0, 0.8);
        
        self.alpha = 0;
        [UIView animateWithDuration: 0.4f animations:^{
            self.alpha = 1;
        }];
    }
    return self;
}
- (void)onTap
{
    [UIView animateWithDuration: 0.4f animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (UITableView *)tableView {
    if (nil == _tableView) {
        _tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, ScreenHeight-UI_TAB_BOTTOM_SAFE_PADDING-240, ScreenWidth, 240) style: UITableViewStylePlain];
        _tableView.backgroundColor = RGB(243, 243, 247);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
        _tableView.sectionFooterHeight = 0.f;
        _tableView.editing = NO;
        [_tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: @"UITableViewCell"];
    }
    return _tableView;
}
#pragma mark - Table View Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}


#pragma mark-UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @"UITableViewCell" forIndexPath: indexPath];
    cell.textLabel.text = self.dataSource[indexPath.row];
    if (indexPath.row == self.dataSource.count-1) {
        cell.textLabel.textColor = [UIColor redColor];
    }else{
        cell.textLabel.textColor = [UIColor blackColor];
    }
    cell.textLabel.font = [UIFont systemFontOfSize: 16];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.dataSource.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.submit && indexPath.row != self.dataSource.count-1) {
        self.submit(indexPath.row);
    }
    [UIView animateWithDuration: 0.4f animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
